package scripts;
import javax.swing.JOptionPane;
import bot.ScriptMethods;

public class GuildFisher extends ScriptMethods implements Runnable {

	enum fishEnum {
		lobster, swordfish, shark
	};

	public void start() {
		System.gc();
		getAccountSettings();
		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		System.gc();
		scriptRunning = false;
		scriptThread = null;
		debug("Stopped script");
	}

	public void run() {

		boolean cook;

		Object[] options = { "Lobster", "Swordfish", "Shark" };
		Object[] options2 = { "Cooked", "Raw" };
		
		String fishType = (String) JOptionPane.showInputDialog(null, "",
				"What will we fish?", JOptionPane.PLAIN_MESSAGE, null, options,
				options[0]);
		
		String cookRaw = (String) JOptionPane.showInputDialog(null, "",
				"Cooked or raw??", JOptionPane.PLAIN_MESSAGE, null, options2,
				options2[0]);
		
		int[] fishArray = new int[9];
		fishArray = (checkFishType(fishType));

		if (cookRaw.equals("Cooked")) {
			cook = true;
		} else {
			cook = false;
		}

		while (scriptRunning) {
			try {
				handleEvents();
				
				if (getCurrentLevel()[7] < fishArray[7]
						|| getCurrentLevel()[10] < fishArray[6]) {
					debug(fishType + " requires:");
					debug("Fishing LvL: " + fishArray[6]);
					debug("Cooking LvL: " + fishArray[7]);
					stop();
				}

				if (!invContains(fishArray[5])) {
					debug("You don't have the required fishing tool.");
					stop();
				}
				
				if (questMenuOpen() && !isSleeping()) {
					answerQuestMenuStringContains("fish to trade", false);
					if (cook == true) {
						answerQuestMenuStringContains(fishType, false);
					} else {
						answerQuestMenuStringContains("Raw " + fishType, false);
					}
					answerQuestMenuStringContains("Twentyfive", true);
					wait(random(1500, 2000));
					
				} else {
					
					if (!isSleeping()) {
						if (itemCount(fishArray[1]) >= 25 && cook == true) {
							goToCert(fishArray[8]);
						} else if (itemCount(fishArray[0]) >= 25 && cook == false) {
							goToCert(fishArray[8]);
						} else {
							if (getInventorySize() < 30) {
								goToFish(fishArray[3], fishArray[4]);
							} else {
								if (invContains(366)) {
									while (invContains(366)) {
										handleEvents();
										dropItem(366);
										wait(random(1000, 1300));
									}
								} else {
									if (cook == true && invContains(fishArray[0])) {
										goToCook(fishArray[0], fishArray[1], fishArray[2]);
									} else {
										if (itemCount(fishArray[1]) >= 25 && !invContains(fishArray[0])) {
											goToCert(fishArray[8]);
										}
									}
								}
							}
						}
					}
				}
				
				wait(50);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
		if (getFatigue() > 80 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
		if (getFightMode() != fightMode)
			setFightMode();
	}

	public void goToFish(int spotID, int clickNum) throws Exception {
		handleEvents();
		int[] fishingSpot = getObjectById(spotID);
		if (fishingSpot[1] != -1) {
			if (clickNum == 1) {
				atObject(fishingSpot[1], fishingSpot[2]);
				wait(random(600, 900));
			} else if (clickNum == 2) {
				atObject2(fishingSpot[1], fishingSpot[2]);
				wait(random(600, 900));
			}
		} else {
			walkToWait(590, 502);
		}
	}

	public void goToCook(int rawID, int cookedID, int burnedID) throws Exception {
		if (ourPlayerY() > 518) {
			int[] range = getObjectById(11);
			if (range[0] != -1) {
				while (invContains(rawID) && !isSleeping()) {
					handleEvents();
					useItemOnObject(range[1], range[2], rawID);
					wait(random(600, 900));
					if (invContains(burnedID)) {
						dropItem(burnedID);
						wait(random(800, 1000));
					}
				}
			}
		} else {
			int[] doors = getObjectById(411);
			if (doors[1] != -1) {
				atObject(doors[1], doors[2]);
				wait(random(1000, 1200));
			}
			walkToWait(584, 520);
		}
	}

	public void goToCert(int npcID) throws Exception {
		handleEvents();
		if (ourPlayerY() > 504 && ourPlayerX() < 602) {
			try {
				int[] doors = getObjectById(411);
				if (doors[1] != -1) {
					atObject(doors[1], doors[2]);
					wait(random(1000, 1200));
				} else {
					walkToWait(603, 504);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			talkToNpc(new int[] { npcID });
			wait(random(1000, 1200));
		}
	}

	public static int[] checkFishType(String arg) {

		fishEnum fe = fishEnum.valueOf(arg.toLowerCase());
		int[] val = new int[9];

		switch (fe) {

		case lobster:
			val[0] = 372; // RawID
			val[1] = 373; // CookedID
			val[2] = 374; // BurntID
			val[3] = 376; // SpotID
			val[4] = 1; // Click#
			val[5] = 375; // Fishing Tool
			val[6] = 40; // FishLvL
			val[7] = 40; // CookLvL
			val[8] = 369; // CertNPC
			break;

		case swordfish:
			val[0] = 369; // RawID
			val[1] = 370; // CookedID
			val[2] = 371; // BurntID
			val[3] = 194; // SpotID
			val[4] = 1; // Click#
			val[5] = 379; // Fishing Tool
			val[6] = 50; // FishLvL
			val[7] = 45; // CookLvL
			val[8] = 369; // CertNPC
			break;

		case shark:
			val[0] = 545; // RawID
			val[1] = 546; // CookedID
			val[2] = 547; // BurntID
			val[3] = 261; // SpotID
			val[4] = 2; // Click#
			val[5] = 379; // Fishing Tool
			val[6] = 76; // FishLvL
			val[7] = 80; // CookLvL
			val[8] = 370; // CertNPC
			break;

		}
		return val;
	}
}